import cplex
import random
import numpy as np
import time
from stoch_trnsport_gen import *
from cplex.exceptions import CplexError
import cplex.callbacks as CPX_CB

class MySolve(CPX_CB.SolveCallback):
    def __call__(self):
        self.times_called += 1
        print "hello"
        self.solve()
#        print "Lower bounds", prob_ref.variables.get_upper_bounds()
#        print prob_ref.solution.get_status()
 #       print prob_ref.solution.basis.get_basis()


NI = 10
NJ = 10
NW = 10
I = range(NI)
J = range(NJ)
O = range(NW)

prob,demand = gen_stoch_trnsport(NI, NJ, NW, eps=0.2)
#prob.solve()
prob_ref = prob
solve_instance = prob.register_callback(MySolve)
solve_instance.times_called = 0

flag = 0
m0 = 0.01

bigm_row_name = [[0]*NJ]*NW
bigm_col_name = [[0]*NJ]*NW
bigm_vals = [m0]* NJ*NW

for w in O:
    for j in J:
        bigm_row_name[w][j] = 'scenario' + str(w) + '_' + 'customer' + str(j)
        bigm_col_name[w][j] = 'y' + '_' + str(w)


while flag == 0:
    # change the big-Ms 
    # prob.linear_constraints.set_coefficients( zip(bigm_row_name,, bigm_col_name, bigm_vals) )
    # prob.lienar_constraints.set_rhs( zip(bigm_row_name, bigm_vals) )

    # solve the MIP
    start_time = time.time()
    prob.solve()
    use_time = time.time() - start_time

    flag = 1


"""
for k in range(len(bigm_vec)):

    row_name = []
    col_name = []
    bigm_coefs = []
    rhs_vals = []

    prob.linear_constraints.set_coefficients( zip(row_name, col_name, bigm_coefs) )
    prob.linear_constraints.set_rhs( zip(row_name, rhs_vals) )
#    prob.linear_constraints.set_coefficients(row_name, col_name, -bigm_vec[k] * demand[w][j])
#    prob.linear_constraints.set_rhs(row_name, demand[w][j] * (1 - bigm_vec[k]) )


    start_time = time.time()
    prob.solve()
    use_time = time.time() - start_time


    print prob.solution.get_objective_value(), 
    print prob.solution.progress.get_num_iterations(),
    print use_time,
    print bigm_vec[k]
"""
    
#vals_arr[l] = prob.solution.get_objective_value()
#time_arr[l, k] = use_time
#vals_arr[l, k] = prob.solution.get_objective_value()
"""
temp_opt_val = prob.solution.get_objective_value()

if abs(temp_opt_val - true_opt_val) <= 0.000001 and use_time <= best_use_time:

    best_use_time = use_time
    x_bigm_star = X_bigm_vec[l]
    y_bigm_star = Y_bigm_vec[k]
""" 

#print X_bigm_vec[l], Y_bigm_vec[k]


